import mysql from 'mysql2';
import {
  MYSQL_HOST,
  MYSQL_PORT,
  MYSQL_USER,
  MYSQL_PASSWORD,
  MYSQL_DATABASE
} from '../app.config';

/**
 * 创建数据服务连接
 */
// export const connection = mysql.createConnection({
//   host: MYSQL_HOST,
//   port: parseInt(MYSQL_PORT, 10),
//   user: MYSQL_USER,
//   password: MYSQL_PASSWORD,
//   database: MYSQL_DATABASE
// })
let dbConfig = {
  host: MYSQL_HOST,
  port: parseInt(MYSQL_PORT, 10),
  user: MYSQL_USER,
  password: MYSQL_PASSWORD,
  database: MYSQL_DATABASE
}

let conn = null

const getConnection = ()=>{
  close()
  return conn = mysql.createConnection(dbConfig)
}

export const close = () => {
  if (conn != null) conn.end()
}

export const connection = getConnection()

